<template>
    <div>
        <el-form ref="form" :model="form"  status-icon  :rules="rules">
          <el-form-item label="当前体温" prop="temperature">
              <el-input v-model="form.temperature" placeholder="输入您当前的体温，精确到小数点后一位"></el-input>
          </el-form-item>
          <el-form-item label="健康状况" prop="health">
              <el-radio-group v-model="form.health">
                <el-radio :label="0">健康</el-radio>
                <el-radio :label="1">不健康</el-radio>
              </el-radio-group>
          </el-form-item>
          <el-form-item label="是否外出" prop="outside">
              <el-input v-model="form.outside"></el-input>
          </el-form-item>
          <el-form-item label="工作状态" prop="work">
              <el-input v-model="form.work"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark"></el-input>
          </el-form-item>
          <el-form-item>
              <el-button type="primary" @click="insertNewRegister('form')">确认</el-button>
              <el-button>取消</el-button>
          </el-form-item>
        </el-form>
    </div>
</template>
<script>

import register from '@/api/register'
export default{
name:'register',
data(){
    var checkTemperature = (rule,value,callback) =>{
        
        let reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g;
        if (value === '') {
            return callback(new Error('请填写体温！'));
        } else if (!reg.test(value)) {
            return callback(new Error('请正确填写'));
        } else if (value > 50 || value < 30){
            return callback(new Error('您的体温填写有误！'))
        }
        else return callback();
    }
    
    return {
        form:{
            temperature:'',
            health:'',
            outside:'',
            work:'',
            remark:'',
        },
        rules: {
            temperature:[{validator: checkTemperature,trigger:'blur'}],
        }
    }

},

methods:{
    insertNewRegister(form){
        if (this.form.health === 0){
            this.form.health = "健康"
        } else {
            this.form.health = "不健康"
        }
        const data = {
            id:localStorage.getItem('ms_userId'),
            college:localStorage.getItem('ms_userCollege'),
            name:localStorage.getItem('ms_username'),
            health:this.form.health,
            temperature:this.form.temperature,
            outside:this.form.outside,
            work:this.form.work,
            remark:this.form.remark
        }
        register.insertNewRegister(data.id,data.college,data.name,
        data.health,data.temperature,data.outside,data.work,data.remark)
        .then(response =>{
            var msg = response.status
            if (msg === 200){
                this.$notify({
                    title: '成功',
                    message: '添加成功！',
                    position: 'top-left',
                    duration: 1500,
                    type: 'success'
                });
            } else {
                this.$notify.error({
                    title: '失败了,稍后再试',
                    position: 'top-left',
                    duration: 1500,
                    message: '失败了'
                });
            }
            this.$refs[form].resetFields();
        })
    }
}
}

</script>